package com.google.code.sip.ucenter.ws.resources;

import java.util.ArrayList;
import java.util.List;

import javax.ws.rs.Consumes;
import javax.ws.rs.FormParam;
import javax.ws.rs.POST;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType;
import javax.xml.ws.Action;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.google.code.sip.core.dao.Page;
import com.google.code.sip.ucenter.model.Account;
import com.google.code.sip.ucenter.model.AccountLog;
import com.google.code.sip.ucenter.model.User;
import com.google.code.sip.ucenter.service.AccountLogManager;
import com.google.code.sip.ucenter.service.AccountManager;
import com.google.code.sip.ucenter.service.UserManager;
import com.google.code.sip.ucenter.ws.WSResponse;
import com.google.code.sip.ucenter.ws.WSResponseStatus;
import com.google.code.sip.ucenter.ws.dto.HuoshowAccountLogDto;
import com.google.code.sip.ucenter.ws.dto.PageDto;
import com.sun.jersey.spi.resource.Singleton;

@Component
@Path("/huoshowAccount")
@Singleton
@Produces( { MediaType.APPLICATION_XML, MediaType.APPLICATION_JSON })
@Consumes(MediaType.APPLICATION_FORM_URLENCODED)
public class HuoshowAccountResource extends AbstractResource {
	private final Log logger = LogFactory.getLog(getClass());
	@Autowired
	private AccountManager huoshowAccountManager;

	@Autowired
	private AccountLogManager huoshowAccountLogManager;

	@Autowired
	private UserManager userManager;

	@POST
	@Action
	@Path("/getBalance")
	public WSResponse<String> getBalance(@FormParam("userId") String userId) {
		assert (null != userId) : "userId cann't be null.";
		try {
			User user = userManager.getUser(userId);
			if (user == null) {
				return new WSResponse<String>(new WSResponseStatus(-1,
						"用户id传入错误，找不到用户！"));
			}
			long balance = huoshowAccountManager.getBalance(user);
			WSResponse<String> wsResponse = new WSResponse<String>();
			wsResponse.setStatus(WSResponseStatus.SUCCESS);
			wsResponse.addResult(String.valueOf(balance));
			return wsResponse;
		} catch (Exception e) {
			logger.error(e.getMessage(), e);
			return new WSResponse<String>(WSResponseStatus.SERVER_ERROR);
		}
	}

	@POST
	@Action
	@Path("/history")
	public WSResponse<PageDto<HuoshowAccountLogDto>> getAccountLogs(
			@FormParam("userId") String userId,
			@FormParam("action") String action,
			@FormParam("pageNo") int pageNo, @FormParam("pageSize") int pageSize) {
		assert (null != userId) : "userId cann't be null.";
		assert (null != action) : "action cann't be null.";
		try {
			User user = userManager.getUser(userId);
			if (user == null) {
				return new WSResponse<PageDto<HuoshowAccountLogDto>>(
						new WSResponseStatus(-1, "用户id传入错误，找不到用户！"));
			}
			Page<AccountLog> page = new Page<AccountLog>();
			Account huoshowAccount = huoshowAccountManager.getLogByUser(user);
			page.addFilter("huoshowAccount", huoshowAccount);
			page.addFilter("action", action);
			page.setPageNo(pageNo);
			page.setPageSize(pageSize);
			Page<AccountLog> result = huoshowAccountLogManager
					.getHuoshowAccountLog(page);
			WSResponse<PageDto<HuoshowAccountLogDto>> wsResponse = new WSResponse<PageDto<HuoshowAccountLogDto>>();
			PageDto<HuoshowAccountLogDto> pageDto = new PageDto<HuoshowAccountLogDto>();
			pageDto.setPageNo(result.getPageNo());
			pageDto.setPageSize(result.getPageSize());
			pageDto.setTotalCount(result.getTotalCount());
			List<HuoshowAccountLogDto> list = new ArrayList<HuoshowAccountLogDto>();

			// HuoshowAccountLogDto huoshowAccountLogDto = new
			// HuoshowAccountLogDto();
			// huoshowAccountLogDto.setAction("actopm1");
			// huoshowAccountLogDto.setAfterBalance(444);
			// huoshowAccountLogDto.setAmount(555);
			// huoshowAccountLogDto.setBeforeBalance(222);
			// huoshowAccountLogDto.setComment("asdsad");
			// huoshowAccountLogDto.setCreatedDate(new Date());
			// huoshowAccountLogDto.setHuoshowAccountId("123456");
			// huoshowAccountLogDto.setId("abcdefg");
			// huoshowAccountLogDto.setOperatorId("3sedfasd");
			// huoshowAccountLogDto.setType("in");
			// list.add(huoshowAccountLogDto);

			for (AccountLog huoshowAccountLog : result.getResult()) {
				HuoshowAccountLogDto huoshowAccountLogDto = new HuoshowAccountLogDto();
				huoshowAccountLogDto.setAction(huoshowAccountLog.getAction());
				huoshowAccountLogDto.setAfterBalance(huoshowAccountLog
						.getAfterBalance());
				huoshowAccountLogDto.setAmount(huoshowAccountLog.getAmount());
				huoshowAccountLogDto.setBeforeBalance(huoshowAccountLog
						.getBeforeBalance());
				huoshowAccountLogDto.setComment(huoshowAccountLog.getComment());
				huoshowAccountLogDto.setCreatedDate(huoshowAccountLog
						.getCreatedDate());
				huoshowAccountLogDto.setHuoshowAccountId(huoshowAccountLog
						.getAccount().getId());
				huoshowAccountLogDto.setId(huoshowAccountLog.getId());
				huoshowAccountLogDto.setOperatorId(huoshowAccountLog
						.getOperator().getUsername());
				huoshowAccountLogDto.setType(huoshowAccountLog.getType());
				list.add(huoshowAccountLogDto);
			}
			if (list.size() > 0) {
				pageDto.setResult(list);
			}
			wsResponse.addResult(pageDto);
			wsResponse.setStatus(WSResponseStatus.SUCCESS);
			return wsResponse;
		} catch (Exception e) {
			logger.error(e.getMessage(), e);
			return new WSResponse<PageDto<HuoshowAccountLogDto>>(
					WSResponseStatus.SERVER_ERROR);
		}

	}

	@POST
	@Action
	@Path("/update")
	public WSResponse<String> updateAccount(@FormParam("userId") String userId,
			@FormParam("action") String action,
			@FormParam("amount") long amount, @FormParam("type") String type,
			@FormParam("remark") String remark) {
		assert (null != userId) : "userId cann't be null.";
		assert (null != action) : "action cann't be null.";
		assert (0 != amount) : "amount cann't be null.";
		assert (null != type) : "type cann't be null.";
		try {
			User user = userManager.getUser(userId);
			if (user == null) {
				return new WSResponse<String>(new WSResponseStatus(-1,
						"用户id传入错误，找不到用户！"));
			}
			huoshowAccountManager.updateAccount(user, type, action, amount,
					remark);

			WSResponse<String> wsResponse = new WSResponse<String>();
			wsResponse.setStatus(WSResponseStatus.SUCCESS);
			return wsResponse;
		} catch (Exception e) {
			logger.error(e.getMessage(), e);
			return new WSResponse<String>(WSResponseStatus.SERVER_ERROR);
		}
	}

}
